package mylab.utils.common;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

import lombok.experimental.UtilityClass;

@UtilityClass
public class DateUtil extends cn.hutool.core.date.DateUtil {
    private static final DateTimeFormatter[] DATE_FORMATTERS = {
            DateTimeFormatter.ofPattern("yyyyMMdd"),
            DateTimeFormatter.ofPattern("yyyy-MM-dd"),
            DateTimeFormatter.ofPattern("yyyy/MM/dd"),
            DateTimeFormatter.ofPattern("MM/dd/yyyy"),
            DateTimeFormatter.ofPattern("MM-dd-yyyy"),
            DateTimeFormatter.ofPattern("dd/MM/yyyy"),
            DateTimeFormatter.ofPattern("dd-MM-yyyy"),
    };

    private static final DateTimeFormatter[] TIME_FORMATTERS = {
            DateTimeFormatter.ofPattern("HH:mm:ss"),
            DateTimeFormatter.ofPattern("HH:mm"),
            DateTimeFormatter.ofPattern("HH:mm:ss.SSS"),
    };

    private static final DateTimeFormatter[] DATETIME_FORMATTERS = {
            DateTimeFormatter.ofPattern("yyyy-MM-dd HH"),
            DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"),
            DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"),
            DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"),
            DateTimeFormatter.ofPattern("yyyy/MM/dd HH"),
            DateTimeFormatter.ofPattern("yyyy/MM/dd HH/mm"),
            DateTimeFormatter.ofPattern("yyyy/MM/dd HH/mm/ss"),
            DateTimeFormatter.ofPattern("MM/dd/yyyy HH"),
            DateTimeFormatter.ofPattern("MM/dd/yyyy HH/mm"),
            DateTimeFormatter.ofPattern("MM/dd/yyyy HH/mm/ss"),
            DateTimeFormatter.ofPattern("MM-dd-yyyy HH"),
            DateTimeFormatter.ofPattern("MM-dd-yyyy HH-mm"),
            DateTimeFormatter.ofPattern("MM-dd-yyyy HH-mm-ss"),
            DateTimeFormatter.ofPattern("dd/MM/yyyy HH"),
            DateTimeFormatter.ofPattern("dd/MM/yyyy HH/mm"),
            DateTimeFormatter.ofPattern("dd/MM/yyyy HH/mm/ss"),
            DateTimeFormatter.ofPattern("dd-MM-yyyy HH"),
            DateTimeFormatter.ofPattern("dd-MM-yyyy HH-mm"),
            DateTimeFormatter.ofPattern("dd-MM-yyyy HH-mm-ss"),
    };

    public LocalDate parseDate(String input) {
        for (DateTimeFormatter formatter : DATE_FORMATTERS) {
            try {
                return LocalDate.parse(input, formatter);
            } catch (DateTimeParseException ignored) {
            }
        }
        return null;
    }

    public LocalTime parseTime(String input) {
        for (DateTimeFormatter formatter : TIME_FORMATTERS) {
            try {
                return LocalTime.parse(input, formatter);
            } catch (DateTimeParseException ignored) {
            }
        }
        return null;
    }

    public LocalDateTime parseDateTime(String input) {
        for (DateTimeFormatter formatter : DATETIME_FORMATTERS) {
            try {
                return LocalDateTime.parse(input, formatter);
            } catch (DateTimeParseException ignored) {
            }
        }
        return null;
    }


}
